package com.study.d1_stream;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test4 {
    public static void main(String[] args) {
        //Stream流常见的终结方法
        //终结方法指的是:调用完成后，不会返回新Stream了，没法继续使用流了
        List<Movie> movies = new ArrayList<>();
        movies.add(new Movie(1,"哈哈",20.5,"成龙"));
        movies.add(new Movie(2,"2哈",200.5,"a1龙"));
        movies.add(new Movie(4,"哈3哈",26.5,"s龙"));
        movies.add(new Movie(5,"哈3哈",28.5,"s龙"));
        movies.add(new Movie(3,"哈4哈",0.5,"fds龙"));
        movies.add(new Movie(3,"哈4哈",0.5,"fds龙"));
        //取最大值
        Movie maxMovie = movies.stream().max(Comparator.comparingDouble(Movie::getPrice)).get();
        System.out.println(maxMovie);
        System.out.println("==========================================");
        //取最小值
        Movie minMovie = movies.stream().min(Comparator.comparingDouble(Movie::getPrice)).get();
        System.out.println(minMovie);
        System.out.println("==========================================");
        //收集Stream流：就是把Stream流操作后的结果转回到集合或数组中去返回
        //Stream流：方便操作集合/数组的收段  集合/数组：才是开发中的目的
        Stream<Movie> stream = movies.stream();
        List<Movie> lists = stream.filter(movie -> movie.getName().contains("4")).collect(Collectors.toList());
        for (Movie list : lists) {
            System.out.println(list);
        }
        System.out.println("==========================================");

//        stream.collect(Collectors.toList()); 报错！流只能使用一次
//        stream.collect(Collectors.toSet());
        Stream<Movie> stream2 = movies.stream();
        Object[] array = stream2.toArray();
        System.out.println(Arrays.toString(array));
        Map<String, Double> collect = movies.stream().limit(4).collect(Collectors.toMap(m1 -> m1.getName(), m2 -> m2.getPrice()
        ,(v1,v2)->v2));//解决重复问题 保留后面的
        System.out.println(collect);
    }
}
